<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="150px">
      <el-form-item label="唯一键ID" prop="sysid">
        <el-input
          v-model="queryParams.sysid"
          placeholder="请输入唯一键ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="军分号" prop="miresnum">
        <el-input
          v-model="queryParams.miresnum"
          placeholder="请输入军分号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中图分类号" prop="clnum">
        <el-input
          v-model="queryParams.clnum"
          placeholder="请输入中图分类号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学科分类号" prop="disnum">
        <el-input
          v-model="queryParams.disnum"
          placeholder="请输入学科分类号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文献标识码" prop="docidentcode">
        <el-input
          v-model="queryParams.docidentcode"
          placeholder="请输入文献标识码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中文关键词" prop="chkeywords">
        <el-input
          v-model="queryParams.chkeywords"
          placeholder="请输入中文关键词"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者机构" prop="authorgan">
        <el-input
          v-model="queryParams.authorgan"
          placeholder="请输入作者机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入作者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="篇名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入篇名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="metadataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一键ID" align="center" :show-overflow-tooltip="true" prop="sysid" />
      <af-table-column label="页码数" align="center" :show-overflow-tooltip="true" prop="pagenum" />
      <af-table-column label="军分号" align="center" :show-overflow-tooltip="true" prop="miresnum" />
      <af-table-column label="中图分类号" align="center" :show-overflow-tooltip="true" prop="clnum" />
      <af-table-column label="学科分类号" align="center" :show-overflow-tooltip="true" prop="disnum" />
      <af-table-column label="中文关键词" align="center" :show-overflow-tooltip="true" prop="chkeywords" />
      <af-table-column label="作者机构" align="center" :show-overflow-tooltip="true" prop="authorgan" />
      <af-table-column label="作者" align="center" :show-overflow-tooltip="true" prop="author" />
      <af-table-column label="源刊出版者" align="center" :show-overflow-tooltip="true" prop="sourcepub" />
      <af-table-column label="来源刊期" align="center" :show-overflow-tooltip="true" prop="sourceissue" />
      <af-table-column label="来源刊年" align="center" :show-overflow-tooltip="true" prop="sourceyear" />
      <af-table-column label="来源刊名" align="center" :show-overflow-tooltip="true" prop="sourcetitle" />
      <af-table-column label="副题名" align="center" :show-overflow-tooltip="true" prop="subtitle" />
      <af-table-column label="篇名" align="center" :show-overflow-tooltip="true" prop="title" />
      <af-table-column label="栏目" align="center" :show-overflow-tooltip="true" prop="column" />
      <af-table-column label="数字资源制作者" align="center" :show-overflow-tooltip="true" prop="diresourcepro" />
      <af-table-column label="权限声明" align="center" :show-overflow-tooltip="true" prop="perstatement" />
      <af-table-column label="源刊出版地" align="center" :show-overflow-tooltip="true" prop="sourceplace" />
      <af-table-column label="文章编号" align="center" :show-overflow-tooltip="true" prop="articlenum" />
      <af-table-column label="来源刊ISSN" align="center" :show-overflow-tooltip="true" prop="issn" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程兵学术元数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="页码数" prop="pagenum">
          <el-input v-model="form.pagenum" placeholder="请输入页码数" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="军事信息资源分类号" prop="miresnum">
          <el-input v-model="form.miresnum" placeholder="请输入军事信息资源分类号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="中图分类号" prop="clnum">
          <el-input v-model="form.clnum" placeholder="请输入中图分类号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="学科分类号" prop="disnum">
          <el-input v-model="form.disnum" placeholder="请输入学科分类号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="中文摘要" prop="chabstract">
          <el-input v-model="form.chabstract" type="textarea" placeholder="请输入内容" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="中文关键词" prop="chkeywords">
          <el-input v-model="form.chkeywords" placeholder="请输入中文关键词" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="作者机构" prop="authorgan">
          <el-input v-model="form.authorgan" placeholder="请输入作者机构" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="源刊出版者" prop="sourcepub">
          <el-input v-model="form.sourcepub" placeholder="请输入源刊出版者" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="来源刊期" prop="sourceissue">
          <el-input v-model="form.sourceissue" placeholder="请输入来源刊期" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="来源刊年" prop="sourceyear">
          <el-input v-model="form.sourceyear" placeholder="请输入来源刊年" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="来源刊名" prop="sourcetitle">
          <el-input v-model="form.sourcetitle" placeholder="请输入来源刊名" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="副题名" prop="subtitle">
          <el-input v-model="form.subtitle" placeholder="请输入副题名" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="篇名" prop="title">
          <el-input v-model="form.title" placeholder="请输入篇名" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="栏目" prop="column">
          <el-input v-model="form.column" placeholder="请输入栏目" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="数字资源制作者" prop="diresourcepro">
          <el-input v-model="form.diresourcepro" placeholder="请输入数字资源制作者" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="权限声明" prop="perstatement">
          <el-input v-model="form.perstatement" placeholder="请输入权限声明" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="源刊出版地" prop="sourceplace">
          <el-input v-model="form.sourceplace" placeholder="请输入源刊出版地" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="文章编号" prop="articlenum">
          <el-input v-model="form.articlenum" placeholder="请输入文章编号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="来源刊ISSN" prop="issn">
          <el-input v-model="form.issn" placeholder="请输入来源刊ISSN" style="width: 200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMetadata, getMetadata, delMetadata, addMetadata, updateMetadata, exportMetadata } from "@/api/gcbxs/metadata";

export default {
  name: "Metadata",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程兵学术元数据表格数据
      metadataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysid: null,
        pagenum: null,
        miresnum: null,
        clnum: null,
        disnum: null,
        docidentcode: null,
        chabstract: null,
        chkeywords: null,
        authorgan: null,
        author: null,
        sourcepub: null,
        sourceissue: null,
        sourceyear: null,
        sourcetitle: null,
        subtitle: null,
        title: null,
        enkeywords: null,
        enabstract: null,
        filecontent: null,
        column: null,
        diresourcepro: null,
        perstatement: null,
        sourceplace: null,
        articlenum: null,
        issn: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sysid: [
          { required: true, message: "唯一键ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工程兵学术元数据列表 */
    getList() {
      this.loading = true;
      listMetadata(this.queryParams).then(response => {
        this.metadataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sysid: null,
        pagenum: null,
        miresnum: null,
        clnum: null,
        disnum: null,
        docidentcode: null,
        chabstract: null,
        chkeywords: null,
        authorgan: null,
        author: null,
        sourcepub: null,
        sourceissue: null,
        sourceyear: null,
        sourcetitle: null,
        subtitle: null,
        title: null,
        enkeywords: null,
        enabstract: null,
        filecontent: null,
        column: null,
        diresourcepro: null,
        perstatement: null,
        sourceplace: null,
        articlenum: null,
        issn: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sysid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学术元数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sysid = row.sysid || this.ids
      getMetadata(sysid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学术元数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sysid != null) {
            updateMetadata(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMetadata(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sysids = row.sysid || this.ids;
      this.$modal.confirm('是否确认删除学术元数据编号为"' + sysids + '"的数据项？').then(function() {
        return delMetadata(sysids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有学术元数据数据项？').then(() => {
        this.exportLoading = true;
        return exportMetadata(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
